//
//  AppDelegate.m
//  ToYourHeartNews
//
//  Created by XJing on 16/1/15.
//  Copyright © 2016年 www.lanou3g.com 蓝鸥. All rights reserved.
//

#import "AppDelegate.h"
#import "NewsViewController.h"
#import "VideoViewController.h"
#import "GIFViewController.h"
#import "CommunicationViewController.h"
#import "UMSocial.h"
#import "GuideViewController.h"
#import "MyselfViewController.h"
#import <SMS_SDK/SMSSDK.h>
@interface AppDelegate ()
@property (nonatomic, assign)BOOL isOut;
@end

@implementation AppDelegate
- (void)dealloc
{
    [_tabBar release];
    [super dealloc];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    #pragma mark - SDK短信验证
    //SDK短信验证初始化应用，appKey和appSecret从后台申请得
    [SMSSDK registerApp:@"fd15e115ec9f"
             withSecret:@"5f1c21d0cbf07ac932c4fbb610c38492"];
    
    //
    BOOL isFirst = [[NSUserDefaults standardUserDefaults]boolForKey:@"firstLauch"];
    if (!isFirst) {
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"firstLauch"];
        GuideViewController *guideVC = [[GuideViewController alloc]init];
#pragma mark - 如果第一打开将引导页VC设置为window跟视图
        self.window.rootViewController = guideVC;
        
        //新闻
        NewsViewController *NewsVC = [[NewsViewController alloc]init];
        UINavigationController *navNews = [[UINavigationController alloc]initWithRootViewController:NewsVC];
        self.tabBar = [[UITabBarController alloc]init];
        NewsVC.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"新闻" image:[UIImage imageNamed:@"iconfont-news@2x.png"] tag:1000];
        _tabBar.tabBar.barStyle = UIBarStyleDefault;
//        _tabBar.tabBar.barTintColor = [UIColor blueColor];
        _tabBar.tabBar.tintColor = [UIColor blueColor];
        //视频
        VideoViewController *VideoVC = [[VideoViewController alloc]init];
        UINavigationController *navVideo = [[UINavigationController alloc]initWithRootViewController:VideoVC];
        VideoVC.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"视频" image:[UIImage imageNamed:@"iconfont-shipin3@3x"] tag:2000];
        
        //GIF
        GIFViewController *GIFVC = [[GIFViewController alloc]init];
        UINavigationController *navGIF = [[UINavigationController alloc]initWithRootViewController:GIFVC];
        GIFVC.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"搞笑" image:[UIImage imageNamed:@"iconfont-cubangaoxiaoxiaohua@2x"] tag:3000];
        //社区
        CommunicationViewController *communicationVC = [[CommunicationViewController alloc]init];
        UINavigationController *navCommunicat = [[UINavigationController alloc]initWithRootViewController:communicationVC];
        communicationVC.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"社区" image:[UIImage imageNamed:@"iconfont-chanpin@2x"] tag:4000];
        _tabBar.viewControllers = @[navNews, navVideo, navGIF, navCommunicat];
        //内存释放
        [NewsVC release];
        [VideoVC release];
        [GIFVC release];
        [communicationVC release];
        [navCommunicat release];
        [navGIF release];
        [navNews release];
        [navVideo release];
        [_tabBar release];

    }else{
    
    //友盟
    [UMSocialData setAppKey:@"56a877ab67e58e508f000fb1"];
    //新闻
    NewsViewController *NewsVC = [[NewsViewController alloc]init];
    UINavigationController *navNews = [[UINavigationController alloc]initWithRootViewController:NewsVC];
    self.tabBar = [[UITabBarController alloc]init];
    NewsVC.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"新闻" image:[UIImage imageNamed:@"iconfont-news@2x.png"] tag:1000];
        _tabBar.tabBar.barStyle = UIBarStyleDefault;
//        _tabBar.tabBar.barTintColor = [UIColor blueColor];
        _tabBar.tabBar.tintColor = [UIColor blueColor];
    //视频
    VideoViewController *VideoVC = [[VideoViewController alloc]init];
    UINavigationController *navVideo = [[UINavigationController alloc]initWithRootViewController:VideoVC];
    VideoVC.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"视频" image:[UIImage imageNamed:@"iconfont-shipin3@3x"] tag:2000];
    
    //GIF
    GIFViewController *GIFVC = [[GIFViewController alloc]init];
    UINavigationController *navGIF = [[UINavigationController alloc]initWithRootViewController:GIFVC];
    GIFVC.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"搞笑" image:[UIImage imageNamed:@"iconfont-cubangaoxiaoxiaohua@2x"] tag:3000];
    //社区
    CommunicationViewController *communicationVC = [[CommunicationViewController alloc]init];
    UINavigationController *navCommunicat = [[UINavigationController alloc]initWithRootViewController:communicationVC];
    communicationVC.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"社区" image:[UIImage imageNamed:@"iconfont-chanpin@2x"] tag:4000];
    _tabBar.viewControllers = @[navNews, navVideo, navGIF, navCommunicat];
    self.window.rootViewController = _tabBar;
    [self.window release];
    //内存释放
    [NewsVC release];
    [VideoVC release];
    [GIFVC release];
    [communicationVC release];
    [navCommunicat release];
    [navGIF release];
    [navNews release];
    [navVideo release];
    [_tabBar release];
    }
//    _tabBar.tabBar.tintColor = [UIColor blueColor];
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // 更新显示的徽章个数
    NSInteger badge = [UIApplication sharedApplication].applicationIconBadgeNumber;
    badge--;
    badge = badge >= 0 ? badge : 0;
    [UIApplication sharedApplication].applicationIconBadgeNumber = badge;
    
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
// 本地通知回调函数，当应用程序在前台时调用
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
    NSLog(@"noti:%@",notification);
    
    // 这里真实需要处理交互的地方
    // 获取通知所带的数据
    NSString *notMess = [notification.userInfo objectForKey:@"key"];
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"通知"
                                                    message:notMess
                                                   delegate:nil
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil];
    [alert show];
    
    // 更新显示的徽章个数
    NSInteger badge = [UIApplication sharedApplication].applicationIconBadgeNumber;
    badge--;
    badge = badge >= 0 ? badge : 0;
    [UIApplication sharedApplication].applicationIconBadgeNumber = badge;
    
    // 在不需要再推送时，可以取消推送
    [MyselfViewController cancelLocalNotificationWithKey:@"key"];
}

@end
